package p2p.peer;

import java.util.Set;
import java.util.UUID;

import p2p.logic.connection.Connection;
import p2p.logic.connection.ConnectionOwner;
import p2p.logic.connection.client.Client;
import p2p.logic.connection.communication.gateway.Gateway;
import p2p.logic.connection.communication.message.Message;
import p2p.logic.connection.communication.type.Send;
import p2p.simulation.PeerManager;

public class LocalGateway extends Gateway {

	
	public LocalGateway(String ip, int port,ConnectionOwner owner) {
		super(ip,port,owner);
	}

	@Override
	public void send(Client client, Message message) {
		
		if (message.getCommand() == 0){
			
			Peer clientPeer = new Peer(client.getPort());
			
			PeerManager.getInstance().addPeerAndSimulate(clientPeer);
			
			new Thread(clientPeer, "Clientpeer " + Peer.count).start();
		}

		Connection send = new Connection(new Send(this.owner.getCommunicationHandler(), message), false);

		new Thread(send, "Send").start();
		
	}

	@Override
	public void send(Set<Client> clients) {
		
	}

	@Override
	public void send(Client client, UUID ack) {
		
		Message ackMessage = new Message(this.owner.getGateway().getIp(), this.owner.getGateway().getPort(), client.getIp(), client.getPort(), ack);
		
		Connection send = new Connection(new Send(this.owner.getCommunicationHandler(), ackMessage), false);

		new Thread(send, "Response").start();
	}	

}
